package com.ht.sps;


import com.ht.sps.nh.entity.TreeMenu;
import com.ht.sps.nh.entity.User;
import com.ht.sps.nh.entity.UserPermission;
import com.ht.sps.nh.service.NhUserService;
import com.ht.sps.nh.service.TreeMenuService;
import com.ht.sps.service.BaseService;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Times;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 王利 14-12-26 上午12:12
 * To change this template use File | Settings | File Templates.
 */
public class MvcSetup implements Setup {
    private static final Logger log = LoggerFactory.getLogger(MvcSetup.class);
    private boolean dropTable = false;
    private Dao dao;
    private Ioc ioc;

    public void init(NutConfig config) {
        ioc = config.getIoc();

        DataSource dataSource = ioc.get(DataSource.class);

        dao = ioc.get(Dao.class, "dao");

        createUserData(dao);

        initEntity(TreeMenu.class, TreeMenuService.class);
        initEntity(UserPermission.class, NhUserService.class);

    }// init end

    private <T, S> void initEntity(Class<T> entity, Class<? extends BaseService> entityService) {
        if (!dao.exists(entity)) {// 表不存在: 先创建表，再初始化数据
            dao.create(entity, false);
            ioc.get(entityService).init();
        }  else if (dropTable) {// 表存在，需要drop: 先drop表，再创建表，再初始化数据
            dao.create(entity, true);
            ioc.get(entityService).init();
        }

    }

    private void createUserData(Dao dao) {
        // 若必要的数据表不存在，则初始化数据库
        if (!dao.exists(User.class)) {
            dao.create(User.class, true);
            createUser(dao, "admin", new Integer[] { 1 });
        }
    }
    private void createUser(Dao dao, String name, Integer[] ids) {
        RandomNumberGenerator rng = new SecureRandomNumberGenerator();
        String salt = rng.nextBytes().toBase64();
        String hashedPasswordBase64 = new Sha256Hash(name, salt, 1024)
                .toBase64();
        User user = new User();
        user.setAccountEnabled(true);
        user.setAccountExpired(false);
        user.setAccountLocked(false);
        user.setCredentialsExpired(false);
        user.setDepartment("生产部");
        user.setMobilePhone("");
        user.setPhone("");
        user.setLoginDate(Times.now());
        user.setCreateDate(Times.now());
        user.setLoginFailureCount(0);
        user.setLoginIP("127.0.0.1");
        user.setModifyDate(Times.now());
        user.setName(name);
        user.setPassword(hashedPasswordBase64);
        user.setSalt(salt);
        user.setLockedDate(Times.now());
        user.setUsername(name);
        user.setType(1);
        user = dao.insert(user);
    }


    @Override
    public void destroy(NutConfig nc) {
        //To change body of implemented methods use File | Settings | File Templates.
        log.info("程序结束");
    }
}
